/**
 * Copyright (c) 华南农业大学信息学院蔡超敏2015版权所有
 * 
 * 文件创建时间：2015年4月22日
 */
package org.scauinfo.fpos.dao.impl;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.scauinfo.fpos.dao.HibernateDao;
import org.scauinfo.fpos.vo.Saleorder;
import org.springframework.stereotype.Repository;

/**
 * @author 蔡超敏
 * 
 */
@Repository
public class SaleorderDao extends HibernateDao<Saleorder> {

	public List<Saleorder> findAllSaleorder(String memberId, String begindate,
			String enddate, int page, int rows, String sort, String order) {
		Criteria criteria = getCurrentSession().createCriteria(getEntity());
		Criteria memberCriteria=criteria.createCriteria("member");
		
		if (!isNull(memberId)) {
			System.out.println("!isNull(memberId)" + !isNull(memberId));
			memberCriteria.add(Restrictions.like("memberId", memberId,
					MatchMode.ANYWHERE));
		}
		if (!isNull(begindate)) {
			System.out.println("!isNull(begindate)" + !isNull(begindate));
			criteria.add(Restrictions.ge("createdDatetime",
					Timestamp.valueOf(begindate + " 00:00:00")));
		}
		if (!isNull(enddate)) {
			System.out.println("!isNull(enddate)" + !isNull(enddate));
			criteria.add(Restrictions.le("createdDatetime",
					Timestamp.valueOf(enddate + " 00:00:00")));
		}

		criteria.setFirstResult((page - 1) * rows);
		criteria.setMaxResults(rows);
		if ("asc".equals(order)) {
			criteria.addOrder(Order.asc(sort));
		} else {
			criteria.addOrder(Order.desc(sort));
		}
		return criteria.list();
	}
	
	public List<Saleorder> findAllSaleorder(String memberId, String begindate,
			String enddate) {
		Criteria criteria = getCurrentSession().createCriteria(getEntity());
		Criteria memberCriteria=criteria.createCriteria("member");
		
		if (!isNull(memberId)) {
			System.out.println("!isNull(memberId)" + !isNull(memberId));
			memberCriteria.add(Restrictions.like("memberId", memberId,
					MatchMode.ANYWHERE));
		}
		if (!isNull(begindate)) {
			System.out.println("!isNull(begindate)" + !isNull(begindate));
			criteria.add(Restrictions.ge("createdDatetime",
					Timestamp.valueOf(begindate + " 00:00:00")));
		}
		if (!isNull(enddate)) {
			System.out.println("!isNull(enddate)" + !isNull(enddate));
			criteria.add(Restrictions.le("createdDatetime",
					Timestamp.valueOf(enddate + " 00:00:00")));
		}
		return criteria.list();
	}

	public boolean isNull(String value) {
		return value == null || value.equals("");
	}
}
